package com.qf.flashlib.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * 2019/4/25 11:18
 */
public class RVAdapter<T> extends RecyclerView.Adapter<RViewHolder> {
    private AdapterHelper<T> helper;

    protected List<T> datas;

    private OnItemClickListener onItemClickListener;

    private OnItemLongClickListener onItemLongClickListener;

    private OnItemChildClickListener onItemChildClickListener;

    private int singleChosenPosition = -1;

    private boolean needWatch = false;

    public RVAdapter() {
        this.helper = new AdapterHelper<>();
        this.datas = new ArrayList<>();
    }

    public RVAdapter(List<T> datas) {
        this.helper = new AdapterHelper<>();
        this.datas = datas == null ? new ArrayList<T>() : datas;
    }

    public RVAdapter(List<T> datas, RViewItem<T> rViewItem) {
        this.helper = new AdapterHelper<>();
        this.datas = datas == null ? new ArrayList<T>() : datas;
        addItemViewType(rViewItem);
    }

    @SafeVarargs
    public RVAdapter(RViewItem<T>... rViewItem) {
        this.helper = new AdapterHelper<>();
        this.datas = new ArrayList<>();
        for (RViewItem<T> trViewItem : rViewItem) {
            addItemViewType(trViewItem);
        }
    }

    public void addItemViewType(RViewItem<T> rViewItem) {
        helper.addItemViewType(rViewItem);
    }

    @NonNull
    @Override
    public RViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RViewItem<T> rView = helper.getRView(viewType);
        int itemLayout = rView.getItemLayout();
        RViewHolder rViewHolder = RViewHolder.createRViewHolder(parent.getContext(), parent, itemLayout);
        rViewHolder.setAdapter(this);
        if (rView.isOpenClick())
            bindViewClickListener(rViewHolder);
        return rViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RViewHolder holder, int position) {
        convert(holder, position);
    }

    private void convert(RViewHolder holder, int position) {
        helper.convert(holder, datas.get(position), position, getDatas().size());
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (helper.getItemViewTypeCount() > 1)
            return helper.getItemViewType(datas.get(position), position);
        return super.getItemViewType(position);
    }

    @Override
    public void onViewAttachedToWindow(@NonNull RViewHolder holder) {
        if (!needWatch)
            return;
        int itemViewType = holder.getItemViewType();
        RViewItem<T> rView = helper.getRView(itemViewType);
        rView.onViewAttachToWindow(holder);
    }

    @Override
    public void onViewDetachedFromWindow(@NonNull RViewHolder holder) {
        if (!needWatch)
            return;
        int itemViewType = holder.getItemViewType();
        RViewItem<T> rView = helper.getRView(itemViewType);
        rView.onViewDetachedFromWindow(holder);
    }

    private void bindViewClickListener(final RViewHolder holder) {
        if (getOnItemClickListener() != null) {
            final View view = holder.getmConvertView();
            if (view == null) {
                return;
            }
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getOnItemClickListener().onItemClick(view, holder.getLayoutPosition());
                }
            });

        }
        if (getOnItemLongClickListener() != null) {
            final View view = holder.getmConvertView();
            if (view == null) {
                return;
            }
            view.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    getOnItemLongClickListener().onItemLongClick(view, holder.getLayoutPosition());
                    return true;
                }
            });

        }
    }

    public List<T> getDatas() {
        return datas == null ? new ArrayList<T>() : datas;
    }

    public T getData(int position) {
        return datas == null ? null : datas.get(position);
    }

    public void setDatas(List<T> datas) {
        this.datas= datas;
        notifyDataSetChanged();
    }

    public void addData(List<T> data) {
        this.datas.addAll(data);
        notifyDataSetChanged();
    }

    /**
     * 用于单选时
     * @return 选中的位置
     */
    public int getSingleChosenPosition() {
        return singleChosenPosition;
    }

    /**
     * 设置选中的位置
     * @param position p
     */
    public void setSingleChosenPosition(int position) {
        this.singleChosenPosition = position;
    }

    @Override
    public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if (manager instanceof GridLayoutManager) {
            GridLayoutManager gridManager = ((GridLayoutManager) manager);
            gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    int itemViewType = helper.getItemViewType(datas.get(position), position);
                    RViewItem<T> rView = helper.getRView(itemViewType);
                    return rView.getSpanSize();
                }
            });
        }
    }

    private OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    private OnItemLongClickListener getOnItemLongClickListener() {
        return onItemLongClickListener;
    }

    public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
        this.onItemLongClickListener = onItemLongClickListener;
    }

    OnItemChildClickListener getOnItemChildClickListener() {
        return onItemChildClickListener;
    }

    public void setOnItemChildClickListener(OnItemChildClickListener onItemChildClickListener) {
        this.onItemChildClickListener = onItemChildClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public interface OnItemChildClickListener {
        void onItemChildClick(View view, int position);
    }

    public interface OnItemLongClickListener {
        void onItemLongClick(View view, int position);
    }

    public void setNeedWatch(boolean needWatch) {
        this.needWatch = needWatch;
    }
}
